// org/x/casemodule/mapper/ScheduleSyncMapper.java
package org.x.casemodule.mapper;

import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.x.casemodule.entity.ScheduleSyncEntity;

import java.util.Date;
import java.util.List;

@Mapper
public interface ScheduleSyncMapper extends BaseMapper<ScheduleSyncEntity> {

    default ScheduleSyncEntity findBySessionId(String sessionId) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .where("sync_session_id = ?", sessionId);
        return selectOneByQuery(queryWrapper);
    }

    default List<ScheduleSyncEntity> findNeedSync(Date beforeTime) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .where("last_sync_time < ?", beforeTime)
                .and("last_sync_status = ?", "SUCCESS");
        return selectListByQuery(queryWrapper);
    }

    default List<ScheduleSyncEntity> findByUserAndPlatform(@Param("userId") Long userId,
                                                           @Param("platform") String platform) {
        QueryWrapper queryWrapper = QueryWrapper.create()
                .where("user_id = ?", userId)
                .and("platform = ?", platform);
        return selectListByQuery(queryWrapper);
    }
}
